{extend name="layout/default" /}
{block name="title"}版本新增{/block}
{block name="content"}
<form class="layui-form" action="">
    <div class="mainBox" style="background-color: white;">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">软件名称</label>
                    <div class="layui-input-block">
                        <select name="sid" lay-search lay-filter="getSoftwareInfo">
                            <option value="">直接选择或搜索选择</option>
                            {volist name="softList" id="vo"}
                            <option value="{$vo.id}">{$vo.name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item" id="configView">
                    <div class="layui-input-block">
                        <blockquote class="layui-elem-quote">
                            提示：请先选择软件，自动生成配置表单后，再填写信息。
                        </blockquote>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="0" title="推送" checked>
                        <input type="radio" name="status" value="1" title="不推送">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="form-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script type="text/html" id="configTemplate">
    {{# layui.each(d.config, function(index, item) { }}
        {{# if(item.type == "text") { }}
            <div class="layui-form-item">
                <label class="layui-form-label">{{ item.cnname }}</label>
                <div class="layui-input-block">
                    <input type="text" name="config[{{ item.enname }}]" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
        {{# } else if(item.type == "textarea") { }}
            <div class="layui-form-item">
                <label class="layui-form-label">{{ item.cnname }}</label>
                <div class="layui-input-block">
                    <textarea name="config[{{ item.enname }}]" class="layui-textarea"></textarea>
                </div>
            </div>
        {{# } else if(item.type == "radio") { }}
            <div class="layui-form-item">
                <label class="layui-form-label">{{ item.cnname }}</label>
                <div class="layui-input-block">
                    <input type="radio" name="config[{{ item.enname }}]" value="1" title="是" checked>
                    <input type="radio" name="config[{{ item.enname }}]" value="0" title="否">
                </div>
            </div>
        {{# } else if(item.type == "file") { }}
            <div class="layui-form-item layui-row">
                <label class="layui-form-label">{{ item.cnname }}</label>
                <div class="layui-input-block layui-upload layui-col-space5">
                    <div class="layui-col-xs12 layui-col-sm8">
                        <input type="text" name="config[{{ item.enname }}]" placeholder="点击按钮上传文件" autocomplete="off" readonly class="layui-input">
                    </div>
                    <div class="layui-col-xs12 layui-col-sm4">
                        <button type="button" class="pear-btn pear-btn-primary upload" data-name="{{ item.enname }}" id="upload">上传文件</button>
                    </div>
                </div>
            </div>
        {{# } }}
    {{# }); }}
</script>
{/block}
{block name="script"}
<script>
    layui.use(['form','jquery', 'laytpl', 'popup', 'upload', 'button'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let laytpl = layui.laytpl;
        let popup = layui.popup;
        let upload = layui.upload;
        let button = layui.button;

        let MODULE_PATH = "/{:request()->controller()}";

        form.on('submit(form-save)', function(data){
            let loading = layer.load();
            $.ajax({
                url: MODULE_PATH + "/add",
                data: data.field,
                dataType:'json',
                type:'post',
                success:function(res){
                    layer.close(loading);
                    if(Number(res.code) === 0){
                        popup.success(res.msg, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("dataTable");
                        });
                    }else{
                        layer.close(loading);
                        popup.failure(res.msg);
                    }
                },
                error: function (res) {
                    layer.close(loading);
                    popup.failure("网络错误, 请重试");
                }
            })
            return false;
        });

        // 设置配置显示
        function setConfigView(data) {
            let configTemplate = document.getElementById('configTemplate').innerHTML,
                configView = document.getElementById('configView');
            laytpl(configTemplate).render(data, function(html){
                configView.innerHTML = html;
            });
            form.render("radio");
        }

        form.on('select(getSoftwareInfo)', function (data) {
            if(["", 0, "0"].includes(data.value)) {
                setConfigView({});
                return false;
            }
            let loading = layer.load();
            $.ajax({
                url: MODULE_PATH + "/getSoftwareInfo",
                type: "post",
                data: {id: data.value},
                success: function (res) {
                    layer.close(loading);
                    if (Number(res.code) === 0) {
                        setConfigView(res.data);
                        initUpload();
                    } else {
                        setConfigView({});
                        popup.failure(res.msg);
                    }
                },
                error: function (res) {
                    layer.close(loading);
                    setConfigView({});
                    popup.failure("获取失败");
                }
            });
            return false;
        })

        // 初始化上传事件
        function initUpload() {
            let upload_btn_load
                ,that
                ,name;
            upload.render({
                elem: ".upload"
                ,url: '/upload/add'
                ,auto: true
                ,accept: "file"
                ,before: function (obj) {
                    upload_btn_load = button.load({
                        elem:'.upload',
                    });
                }
                ,progress: (n, elem) => {
                    // 获取当前实例 dom
                    that = elem;
                    name = $(that).data("name");
                }
                ,done: (res, index, upload) => {
                    upload_btn_load.stop();
                    if(Number(res.code) === 0) {
                        $("input[name='config["+name+"]']").val(res.data.src);
                        popup.success(res.msg);
                    } else {
                        popup.failure(res.msg);
                    }
                }
                ,error: function () {
                    upload_btn_load.stop();
                }
            });
        }

        let tp_debug = $("#think_page_trace_open");
        (tp_debug.length > 0) && tp_debug.remove();
    })
</script>
{/block}